package com.xiaouyudeguang.common.entity;

import com.xiaouyudeguang.common.utils.JavaType;
//import utils.io.gitee.xiaouyudeguang.common.PingYinUtils;
import com.xiaouyudeguang.common.utils.PingYinUtils;
import com.xiaouyudeguang.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Column {

    private String columnName;

    private String comment;

    private String columnType;

    public String getFieldName() {
        return StringUtils.putOffUnderline(this.columnName);
    }

    public String getJavaType() {
        return JavaType.getJavaType(this.columnType);
    }

    public static List<Column> getColumns(Map<String, String> columns, boolean toPinyin) throws BadHanyuPinyinOutputFormatCombination {
        List<Column> columnList = new ArrayList<>();
        if (columns != null) {
            for (Map.Entry<String, String> entry : columns.entrySet()) {
                String javaType = JavaType.getJavaType(entry.getValue());
                if (StringUtils.isNotBlank(javaType)) {
                    String columnName = toPinyin ? PingYinUtils.transform(entry.getKey()) : entry.getKey();
                    columnList.add(Column.builder().columnName(columnName).comment(entry.getKey()).columnType(entry.getValue()).build());
                } else if (Pattern.compile("[\u4e00-\u9fa5]").matcher(entry.getKey()).find()) {
                    columnList.add(Column.builder().columnName(entry.getValue()).comment(entry.getKey()).columnType("varchar(50)").build());
                } else {
                    columnList.add(Column.builder().columnName(entry.getKey()).comment(entry.getValue()).columnType("varchar(50)").build());
                }
            }
        }
        return columnList;
    }
}